package com.aos.erp.schools.rest.gatherschool;


import com.aos.erp.common.bean.IPageBean;
import com.aos.erp.common.bean.ResultEntity;
import com.aos.erp.common.exception.BizException;
import com.aos.erp.schools.model.gatherschool.entity.GatherSchool;
import com.aos.erp.schools.model.gatherschool.ro.DevRo;
import com.aos.erp.schools.model.gatherschool.ro.GatherSchoolRo;
import com.aos.erp.schools.model.gatherschool.ro.QueryGatherSchoolRo;
import com.aos.erp.schools.model.gatherschool.ro.UpdateGatherRo;
import com.aos.erp.schools.model.gatherschool.vo.*;
import com.aos.erp.schools.service.gatherschool.GatherSchoolService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 傲雄驾校管理 前端控制器
 * </p>
 *
 * @author 杨文博
 * @since 2018-12-02
 */
@Slf4j
@RestController
@RequestMapping("/api/gather")
@Api(value="傲雄驾校管理",tags={"傲雄驾校管理"})
public class GatherSchoolController {

	@Autowired
	private GatherSchoolService gatherSchoolService;

	@GetMapping("/list")
	@ApiOperation(value = "傲雄驾校管理分页查询")
	public ResultEntity<IPageBean<GatherListVo>> queryPageBean(QueryGatherSchoolRo queryGatherSchoolRo) {
		return ResultEntity.success(gatherSchoolService.queryByPage(queryGatherSchoolRo));
	}



	@ApiOperation(value = "新增驾校", notes = "新增驾校")
	@RequestMapping(value = "/add",method = RequestMethod.POST)
	public ResultEntity addGather(@Valid GatherSchoolRo gatherSchoolRo, BindingResult bindingResult) throws Exception {
		//数据 验证信息
		if (bindingResult.hasErrors()) {
			return  ResultEntity.failed(bindingResult.getFieldErrors().get(0).getDefaultMessage());
		}
		GatherUserVo gatherUserVo=gatherSchoolService.addGather(gatherSchoolRo);
		return ResultEntity.success(gatherUserVo);
	}

	@ApiOperation(value = "删除驾校", notes = "删除驾校")
	@RequestMapping(value = "/delete",method = RequestMethod.POST)
	public ResultEntity deleteGather(@RequestParam  @NotNull(message = "参数不能为空") Long id) throws BizException {
		return gatherSchoolService.deleteGather(id)>=1?ResultEntity.success():ResultEntity.failed();
	}

	/**
	 * 查看--获取驾校信息
	 * @[param id  驾校id
	 * @return
	 */
	@ApiOperation(value = "获取驾校信息", notes = "获取驾校信息")
	@RequestMapping(value = "/info", method = RequestMethod.POST)
	@ApiImplicitParam(name = "id",value = "驾校id",required = true,dataType ="Long",paramType ="query" )
	public ResultEntity<GatherInfoVo> gatherInfo(@NotNull(message = "参数不能为空") long id) throws BizException {
		return ResultEntity.success(gatherSchoolService.gatherInfo(id));
	}



	/**
	 * 修改--获取驾校信息
	 * @[param id  驾校id
	 * @return
	 */
	@ApiOperation(value = "修改--获取驾校信息", notes = "修改--获取驾校信息")
	@RequestMapping(value = "/updateInfo", method = RequestMethod.POST)
	@ApiImplicitParam(name = "id",value = "驾校id",required = true,dataType ="Long",paramType ="query" )
	public ResultEntity<UpdateGatherInfoVo> updateInfo(@NotNull(message = "参数不能为空") long id) throws BizException {
		return ResultEntity.success(gatherSchoolService.updateInfo(id));
	}



	/**
	 * 修改驾校
	 * @[param UpdateDivisionRos  驾校实体
	 * @return
	 */
	@ApiOperation(value = "修改驾校", notes = "修改驾校")
	@RequestMapping(value = "/update",method = RequestMethod.POST)
	public ResultEntity updateGather(@Validated UpdateGatherRo updateGatherRo, BindingResult bindingResult){
		//数据 验证信息
		if (bindingResult.hasErrors()) {
			return  ResultEntity.failed(bindingResult.getFieldErrors().get(0).getDefaultMessage());
		}
		gatherSchoolService.updateGather(updateGatherRo);
		return  ResultEntity.success();
	}



	@GetMapping("/getGather")
	@ApiOperation(value = "根据当前用户获取总校信息")
	public ResultEntity getGather(String currentUserId) throws Exception {
		GatherSchool gatherSchool=gatherSchoolService.getGather(currentUserId);
		return ResultEntity.success(gatherSchool);
	}


	@GetMapping("/getTotalGather")
	@ApiOperation(value = "获取总校")
	public ResultEntity getTotalGather() throws Exception {
		List<TotalSchool> totalSchool =gatherSchoolService.getTotalGather();
		return ResultEntity.success(totalSchool);
	}


	@PostMapping("/getBranch")
	@ApiOperation(value = "获取总校下面所有分校")
	@ApiImplicitParam(name = "gatherId",value = "总校id",required = true,dataType ="Long",paramType ="query" )
	public ResultEntity getBranch(String gatherId) throws Exception {
		if(gatherId==null){
			return ResultEntity.failed("请传入参数");
		}
		List<TotalSchool> totalSchool =gatherSchoolService.getBranch(gatherId);
		return ResultEntity.success(totalSchool);
	}

	@PostMapping("/getGatherCodeName")
	@ApiOperation(value = "获取总校代码和名称")
	@ApiImplicitParam(name = "gatherId",value = "总校id",required = true,dataType ="Long",paramType ="query" )
	public ResultEntity getGatherCodeName(String gatherId) throws Exception {
		if(gatherId==null){
			return ResultEntity.failed("请传入参数");
		}
		GatherCodeNameVo gatherCodeNameVo =gatherSchoolService.getGatherCodeName(gatherId);
		return ResultEntity.success(gatherCodeNameVo);
	}

	@PostMapping("/getGatherByName")
	@ApiOperation(value = "根据驾校名称模糊查询驾校")
	@ApiImplicitParam(name = "name",value = "驾校名称",required = true,dataType ="String",paramType ="query" )
	public ResultEntity<List<GatherTypeVo>> getGatherByName(String name) throws Exception {
		if(name==null){
			return ResultEntity.failed("请传入参数");
		}
		List<GatherTypeVo> GatherTypeVo =gatherSchoolService.getGatherByName(name);
		return ResultEntity.success(GatherTypeVo);
	}

	@GetMapping("/getSchoolTree")
	@ApiOperation(value = "查新树形结构")
	@ApiImplicitParam(name = "currentUserId",value = "登录用户ID",dataType ="int" )
	public ResultEntity<List<SchoolTreeVo>> getSchoolTree(Long currentUserId) throws Exception {
		if(currentUserId==null){
			return ResultEntity.failed("请先登录");
		}
		List<SchoolTreeVo> SchoolTree =gatherSchoolService.getSchoolTree(currentUserId);
		return ResultEntity.success(SchoolTree);
	}

	@GetMapping("/getSchoolByDev")
	@ApiOperation(value = "根据设备查询驾校")
//	@ApiImplicitParams({@ApiImplicitParam(name = "usbKey",value = "设备CODE",dataType ="String" ),
//			@ApiImplicitParam(name = "deviceCode",value = "122账号",dataType ="String" )})
	public ResultEntity<List<GatherSchool>>  getSchoolByDev(String usbKey,String deviceCode) throws BizException {
		DevRo devRo=new DevRo();
		devRo.setDeviceCode(deviceCode);
		devRo.setUsbKey(usbKey);
		List<GatherSchool>  SchoolTree=gatherSchoolService.getSchoolByDev(devRo);
		return ResultEntity.success(SchoolTree);
	}


}

